<?php

namespace App\Observers;

use App\Models\Product;
use App\Services\LogService;
use App\Services\CacheService;
use Illuminate\Support\Facades\Log;

class ProductObserver
{
    protected LogService $logService;
    protected CacheService $cacheService;

    public function __construct(LogService $logService, CacheService $cacheService)
    {
        $this->logService = $logService;
        $this->cacheService = $cacheService;
    }

    /**
     * Handle the Product "created" event.
     */
    public function created(Product $product): void
    {
        $this->logService->logProduct('created', $product);
        
        // 清除相关缓存
        $this->clearProductCaches();
        
        Log::info('Product created', [
            'product_id' => $product->id,
            'name' => $product->name,
            'slug' => $product->slug,
            'price' => $product->price,
        ]);
    }

    /**
     * Handle the Product "updated" event.
     */
    public function updated(Product $product): void
    {
        // 检查状态是否发生变化
        if ($product->isDirty('is_active')) {
            $oldStatus = $product->getOriginal('is_active');
            $newStatus = $product->is_active;
            
            $this->logService->logProduct('status_changed', $product, [
                'old_status' => $oldStatus ? 'active' : 'inactive',
                'new_status' => $newStatus ? 'active' : 'inactive',
            ]);

            Log::info('Product status changed', [
                'product_id' => $product->id,
                'name' => $product->name,
                'old_status' => $oldStatus ? 'active' : 'inactive',
                'new_status' => $newStatus ? 'active' : 'inactive',
            ]);
        }

        // 检查价格是否发生变化
        if ($product->isDirty('price')) {
            $oldPrice = $product->getOriginal('price');
            $newPrice = $product->price;
            
            $this->logService->logProduct('price_changed', $product, [
                'old_price' => $oldPrice,
                'new_price' => $newPrice,
            ]);

            Log::info('Product price changed', [
                'product_id' => $product->id,
                'name' => $product->name,
                'old_price' => $oldPrice,
                'new_price' => $newPrice,
            ]);
        }

        // 检查库存是否发生变化
        if ($product->isDirty('stock_quantity')) {
            $oldStock = $product->getOriginal('stock_quantity');
            $newStock = $product->stock_quantity;
            
            $this->logService->logProduct('stock_changed', $product, [
                'old_stock' => $oldStock,
                'new_stock' => $newStock,
            ]);

            Log::info('Product stock changed', [
                'product_id' => $product->id,
                'name' => $product->name,
                'old_stock' => $oldStock,
                'new_stock' => $newStock,
            ]);
        }

        $this->logService->logProduct('updated', $product);
        
        // 清除相关缓存
        $this->clearProductCaches($product);
    }

    /**
     * Handle the Product "deleted" event.
     */
    public function deleted(Product $product): void
    {
        $this->logService->logProduct('deleted', $product);
        
        // 清除相关缓存
        $this->clearProductCaches($product);
        
        Log::info('Product deleted', [
            'product_id' => $product->id,
            'name' => $product->name,
            'slug' => $product->slug,
        ]);
    }

    /**
     * Handle the Product "restored" event.
     */
    public function restored(Product $product): void
    {
        $this->logService->logProduct('restored', $product);
        
        // 清除相关缓存
        $this->clearProductCaches();
        
        Log::info('Product restored', [
            'product_id' => $product->id,
            'name' => $product->name,
            'slug' => $product->slug,
        ]);
    }

    /**
     * Handle the Product "force deleted" event.
     */
    public function forceDeleted(Product $product): void
    {
        $this->logService->logProduct('force_deleted', $product);
        
        // 清除相关缓存
        $this->clearProductCaches($product);
        
        Log::info('Product force deleted', [
            'product_id' => $product->id,
            'name' => $product->name,
            'slug' => $product->slug,
        ]);
    }

    /**
     * 清除产品相关缓存
     */
    protected function clearProductCaches(Product $product = null): void
    {
        try {
            // 清除产品列表缓存
            $this->cacheService->forgetProductsList();
            
            // 清除特定产品缓存
            if ($product) {
                $this->cacheService->forgetProduct($product->id);
            }
            
            // 清除分类缓存
            $this->cacheService->forgetCategories();
            
            Log::debug('Product caches cleared', [
                'product_id' => $product?->id,
            ]);
        } catch (\Exception $e) {
            Log::error('Failed to clear product caches', [
                'product_id' => $product?->id,
                'error' => $e->getMessage(),
            ]);
        }
    }
}
